﻿using System;
using System.Reflection;
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KPIS.GERP.SERVICE.DAL
{
    public class Common
    {
        public static string ApprovedStatus = "A";
        public static string UsedStatus = "U";
        public static string WaitStatus = "W";

        /// <summary>
        /// ทำการส่งรายการไปอนุมัติแล้ว
        /// </summary>
        public static string ProcessStatus = "P";
        public static string DeletedStatus = "D";
        public static string CancelStatus = "C";

        public static string ErrorEmptyData = "ไม่พบข้อมูล";
        public static string ErrorNoUserData = "รหัสผู้ใช้หรือรหัสผ่านไม่ถูกต้อง";

        public Nullable<int> ConvertToNullableInt(object objValue)
        {
            Nullable<int> intResult = null;
            int nResult;

            if (objValue != null && int.TryParse(objValue.ToString(), out nResult))
            {
                intResult = nResult;
            }

            return intResult;
        }

        public Nullable<double> ConvertToNullableDouble(object objValue)
        {
            Nullable<double> dblResult = null;
            double dResult;

            if (objValue != null && double.TryParse(objValue.ToString(), out dResult))
            {
                dblResult = dResult;
            }

            return dblResult;
        }

        public Nullable<decimal> ConvertToNullableDecimal(object objValue)
        {
            Nullable<decimal> dcmResult = null;
            decimal dResult;

            if (objValue != null && decimal.TryParse(objValue.ToString(), out dResult))
            {
                dcmResult = dResult;
            }

            return dcmResult;
        }

        public Nullable<DateTime> ConvertToNullableDateTime(object objValue)
        {
            Nullable<DateTime> dtmResult = null;
            DateTime dtResult;

            if (objValue != null && DateTime.TryParse(objValue.ToString(), out dtResult))
            {
                dtmResult = dtResult;
            }

            return dtmResult;
        }

        public bool IsIntNullOrZero(int? objValue)
        {
            return !objValue.HasValue || objValue.Value == 0;
        }
    }

    public class CollectionHelper
    {
        private CollectionHelper()
        {
        }

        public static DataTable ConvertTo<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (T item in list)
            {
                DataRow row = table.NewRow();

                foreach (PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item);
                }

                table.Rows.Add(row);
            }

            return table;
        }

        public static IList<T> ConvertTo<T>(IList<DataRow> rows)
        {
            IList<T> list = null;

            if (rows != null)
            {
                list = new List<T>();

                foreach (DataRow row in rows)
                {
                    T item = CreateItem<T>(row);
                    list.Add(item);
                }
            }

            return list;
        }

        public static IList<T> ConvertTo<T>(DataTable table)
        {
            if (table == null)
            {
                return null;
            }

            List<DataRow> rows = new List<DataRow>();

            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }

            return ConvertTo<T>(rows);
        }

        public static T CreateItem<T>(DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                //object value2 = null;
                obj = Activator.CreateInstance<T>();

                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);


                    if (prop != null)
                    {
                        try
                        {
                            //int value2 = 11;
                            object value = (row[column.ColumnName] != DBNull.Value ? row[column.ColumnName] : null);
                            //var TypeTTT = typeof();
                            //DateTime dt = new DateTime(DateTime.Now.Year,13,11);
                            //if (column.ColumnName.Equals("birth_date")) value = (object)dt;
                            if (value != null)
                            {
                                Type typeDTB = value.GetType();
                                Type typeList = prop.PropertyType;
                            }
                            if (value != null && value.GetType() == typeof(UInt32))
                            {
                                value = Convert.ToInt32(value);
                            }
                            else if (value != null && value.GetType() == typeof(Int64))
                            {
                                value = Convert.ToDecimal(value);
                            }
                            ////else if (value != null && value.GetType().Name.Equals("MySqlDateTime"))
                            ////{
                            ////    value = Convert.ToDateTime(value);
                            ////}
                            //else if (typeof(value).Name.Equals("MySqlDateTime"))
                            //{
                            //    value = Convert.ToDateTime(value);
                            //}

                            prop.SetValue(obj, value, null);
                        }
                        catch (Exception ex)
                        {
                            // You can log something here
                            throw ex;
                        }
                    }
                }
            }

            return obj;
        }

        public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                table.Columns.Add(prop.Name, prop.PropertyType);
            }

            return table;
        }
    }

}